package com.dingding.nettyserver.common.trasferrule.response;

import com.dingding.nettyserver.common.protocolbuf.ErrorTipsProtocol.ErrorTips;
import com.google.protobuf.MessageLite;

/**
 * @Description 传送给客户端的业务执行结果的封装
 * @author fengshuonan
 * @date 2016年11月17日 上午11:07:12
 */
public class HandlerResult<T>{

	/**
	 * 结果码
	 */
	private int resultCode;
	
	/**
	 * 结果内容
	 */
	private T content;
	
	public static <T extends MessageLite> HandlerResult<T> SUCCESS(T content){
		HandlerResult<T> result = new HandlerResult<T>();
		result.resultCode = ResultCode.SUCCESS.getCode();
		result.content = content;
		return result;
	}
	
	public static <T extends MessageLite> HandlerResult<T> SUCCESS(){
		HandlerResult<T> result = new HandlerResult<T>();
		result.resultCode = ResultCode.SUCCESS.getCode();
		return result;
	}
	
	public static <T extends MessageLite> HandlerResult<T> ERROR(int resultCode){
		HandlerResult<T> result = new HandlerResult<T>();
		result.resultCode = resultCode;
		return result;
	}
	
	public static <T extends MessageLite> HandlerResult<ErrorTips> ERROR(ResultCode code){
		HandlerResult<ErrorTips> result = new HandlerResult<ErrorTips>();
		result.resultCode = code.getCode();
		ErrorTips tips = ErrorTips.newBuilder().setErrorMessage(code.getMessage()).build();
		result.setContent(tips);
		return result;
	}
	
	public static <T extends MessageLite> HandlerResult<T> valueOf(int resultCode, T content){
		HandlerResult<T> result = new HandlerResult<T>();
		result.resultCode = resultCode;
		result.content = content;
		return result;
	}
	

	public int getResultCode() {
		return resultCode;
	}

	public void setResultCode(int resultCode) {
		this.resultCode = resultCode;
	}

	public T getContent() {
		return content;
	}

	public void setContent(T content) {
		this.content = content;
	}
	
	public boolean isSuccess(){
		return this.resultCode == ResultCode.SUCCESS.getCode();
	}

}
